package com.qingmi.farmer.ui.main;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.gyf.barlibrary.ImmersionBar;
import com.qingmi.farmer.BuildConfig;
import com.qingmi.farmer.R;
import com.qingmi.farmer.data.model.UpdateVersion;
import com.wjjun.base.data.preference.UserPreference;
import com.qingmi.farmer.data.repository.data.BasicDataRepository;
import com.qingmi.farmer.databinding.ActivityMainBinding;
import com.wjjun.base.ui.base.BaseActivity;
import com.qingmi.farmer.ui.my.MineFragment;
import com.qingmi.farmer.ui.order.goods.GoodsOrderFragment;
import com.qingmi.farmer.ui.order.grain.GrainOrderFragment;
import com.qingmi.farmer.ui.trolley.TrolleyFragment;
import com.qingmi.farmer.window.UpdateVersionDialog;
import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 首页
 * Created by 67535 on 2018/4/26.
 */

public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> {

    private final static int EXIT_APP_TIME = 2000;

    public static void start(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }


    private List<Fragment> fragments = new ArrayList<>();

    private HomeFragment homeFragment;
    private GoodsOrderFragment goodsOrderFragment;
    private GrainOrderFragment grainOrderFragment;
    private TrolleyFragment trolleyFragment;
    private MineFragment mineFragment;

    private FragmentTransaction ft;

    private Fragment mCurrentFragment;

    @Inject
    BasicDataRepository basicDataRepository;

    @Inject
    UserPreference userPreference;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected MainViewModel initViewModel() {
        return new MainViewModel();
    }

    @Override
    protected void initData() {
        super.initData();

        initFragment();
    }

    private void initFragment() {
        fragments.clear();
        if (homeFragment == null) {
            homeFragment = new HomeFragment();
        }
        if (goodsOrderFragment == null) {
            goodsOrderFragment = new GoodsOrderFragment();
        }
        if (grainOrderFragment == null) {
            grainOrderFragment = new GrainOrderFragment();
        }
        if (trolleyFragment == null) {
            trolleyFragment = new TrolleyFragment();
        }
        if (mineFragment == null) {
            mineFragment = new MineFragment();
        }
        fragments.add(homeFragment);
        fragments.add(goodsOrderFragment);
        fragments.add(grainOrderFragment);
        fragments.add(trolleyFragment);
        fragments.add(mineFragment);

        changeTab(0);
    }

    @Override
    protected void initContent() {
        basicDataRepository.bind(this);
        super.initContent();
        viewModel.setShowAppBar(false);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.rb_home:
                changeTab(0);
                break;
            case R.id.rb_goods_order:
                changeTab(1);
                break;
            case R.id.rb_grain_order:
                changeTab(2);
                break;
            case R.id.rb_trolley:
                changeTab(3);
                break;
            case R.id.rb_mine:
                changeTab(4);
                break;
        }
    }

    protected void changeTab(int position) {
        ft = getSupportFragmentManager().beginTransaction();

        //判断当前的Fragment是否为空，不为空则隐藏
        if (null != mCurrentFragment) {
            ft.hide(mCurrentFragment);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragments.get(position).getClass().getName());

        if (null == fragment) {
            //如fragment为空，则之前未添加此Fragment。便从集合中取出
            fragment = fragments.get(position);
        }
        mCurrentFragment = fragment;

        if (position == 0) {    //首页
            try {
                ImmersionBar.with(this)
                        .reset()
                        .statusBarColor(R.color.white)
                        .statusBarDarkFont(true, 0.7f)
                        .keyboardEnable(true).init(); //初始化，默认透明状态栏和黑色导航栏
            } catch (Exception e) {
                //避免全屏的界面报错
                ImmersionBar.with(this).keyboardEnable(true).init();
            }
        } else {
            try {
                ImmersionBar.with(this)
                        .reset()
                        .statusBarDarkFont(false)
                        .keyboardEnable(true).init(); //初始化，默认透明状态栏和黑色导航栏
            } catch (Exception e) {
                //避免全屏的界面报错
                ImmersionBar.with(this).keyboardEnable(true).init();
            }
        }

        //判断此Fragment是否已经添加到FragmentTransaction事物中
        if (!fragment.isAdded()) {
            ft.add(R.id.fl_content, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commit();
    }


    private long mExitTime;

    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > EXIT_APP_TIME) {
            Toast.makeText(this, R.string.txt_exit_app, Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            ActivityUtils.finishAllActivities();
            finish();
            System.exit(0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
